﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using iTextSharp.text;
using iTextSharp.text.pdf;

using MakeIT.MakeIT.Laundy.Agromotor.Site.Entidades;
using MakeIT.MakeIT.Laundy.Agromotor.Site.Negocios;

namespace MakeIT.MakeIT.Laundy.Agromotor.Site.Apresentacao
{
    public partial class DownCatalogo : System.Web.UI.Page
    {
        protected Font fontRow1
        {
            get
            {
                // create a basecolor to use for the footer font, if needed.
                BaseColor color = new BaseColor(0, 0, 0);
                Font font = FontFactory.GetFont("Arial", 8, Font.BOLD, color);
                return font;
            }
        }
        protected Font fontRow2
        {
            get
            {
                // create a basecolor to use for the footer font, if needed.
                BaseColor color = new BaseColor(0, 0, 0);
                Font font = FontFactory.GetFont("Arial", 7, Font.BOLD, color);
                return font;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string ordem = this.Request["orderBy"];
            if (ordem == null)
                ordem = "desc";

            List<PecaVO> catalogo = AgromotorsiteDAO.getPecas(null, null, null, null, null, null, null,ordem.Equals("desc"));

            createPDF(catalogo, ordem);

        }

        protected void createPDF(List<PecaVO> catalogo, string ordem)
        {
            Response.ContentType = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=CatalogoAgromotor" + DateTime.Today.ToString("ddMMyyyy") + (ordem.Equals("desc") ? "Alfabetica" : "Codigo") + ".pdf");

            
            Document documento = new Document();
            documento.SetMargins(20, 20, 50, 50);

            PdfWriter pdfWriter = PdfWriter.GetInstance(documento, Response.OutputStream);
            pdfWriter.PageEvent = new MyPdfPageEventHelp();

            documento.Open();

            PdfPTable rowTbl = new PdfPTable(2);
            rowTbl.TotalWidth = documento.PageSize.Width;

            PdfPCell cell = new PdfPCell(new Paragraph("Código", fontRow1));
            cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
            cell.Border = PdfPCell.NO_BORDER;
            rowTbl.AddCell(cell);

            cell = new PdfPCell(new Paragraph("Descrição", fontRow1));
            cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
            cell.Border = PdfPCell.NO_BORDER;
            rowTbl.AddCell(cell);

            foreach (PecaVO pVO in catalogo)
            {
                cell = new PdfPCell(new Paragraph(pVO.codigo, fontRow2));
                cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
                cell.Border = PdfPCell.NO_BORDER;
                rowTbl.AddCell(cell);

                cell = new PdfPCell(new Paragraph(pVO.peca, fontRow2));
                cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
                cell.Border = PdfPCell.NO_BORDER;
                rowTbl.AddCell(cell);
            }

            documento.Add(rowTbl);
            
            documento.Close();

            Response.End();
        }
    }


    public class MyPdfPageEventHelp : PdfPageEventHelper
    {
        protected Font fontHeader1
        {
            get
            {
                // create a basecolor to use for the footer font, if needed.
                BaseColor color = new BaseColor(0, 0, 0);
                Font font = FontFactory.GetFont("Arial", 10, Font.BOLD, color);
                return font;
            }
        }
        protected Font fontHeader2
        {
            get
            {
                // create a basecolor to use for the footer font, if needed.
                BaseColor color = new BaseColor(128, 128, 128);
                Font font = FontFactory.GetFont("Arial", 9, Font.NORMAL, color);
                return font;
            }
        }
        protected Font fontFooter1
        {
            get
            {
                // create a basecolor to use for the footer font, if needed.
                BaseColor color = new BaseColor(128, 128, 128);
                Font font = FontFactory.GetFont("Arial", 7, Font.NORMAL, color);
                return font;
            }
        }
        protected Font fontFooter2
        {
            get
            {
                // create a basecolor to use for the footer font, if needed.
                BaseColor color = new BaseColor(0, 0, 0);
                Font font = FontFactory.GetFont("Arial", 8, Font.NORMAL, color);
                return font;
            }
        }
        
        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            base.OnStartPage(writer, document);

        }
        public override void OnStartPage(PdfWriter writer, Document document)
        {
            base.OnStartPage(writer, document);

            PdfPTable headerTbl = new PdfPTable(1);
            headerTbl.TotalWidth = document.PageSize.Width;

            PdfPCell cell = new PdfPCell(new Paragraph("CATÁLOGO DE PEÇAS (SITE)", fontHeader1));
            cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
            cell.Border = PdfPCell.BOTTOM_BORDER;
            headerTbl.AddCell(cell);

            cell = new PdfPCell(new Paragraph("EM CASO DE DÚVIDA OU NECESSIDADE ADICIONAL DE INFORMAÇÃO, ENTRE EM CONTATO COM NOSSOS ATENDENTES.", fontHeader2));
            cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
            cell.Border = PdfPCell.BOTTOM_BORDER;
            headerTbl.AddCell(cell);

            headerTbl.WriteSelectedRows(0, -1, 0, (document.PageSize.Height-20), writer.DirectContent);
        }
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            base.OnEndPage(writer, document);

            PdfPTable footerTbl = new PdfPTable(1);
            footerTbl.TotalWidth = document.PageSize.Width;

            PdfPCell cell = new PdfPCell(new Paragraph("Informações exclusivas para consulta. Alguns ítens em estoque podem não constar nesta relação. Se necessário entre em contato com nossos atendentes.", fontFooter1));
            cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
            cell.Border = PdfPCell.NO_BORDER;
            footerTbl.AddCell(cell);

            cell = new PdfPCell(new Paragraph("Página "+document.PageNumber, fontFooter2));
            cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
            cell.Border = PdfPCell.NO_BORDER;
            cell.PaddingRight = 20;
            footerTbl.AddCell(cell);

            footerTbl.WriteSelectedRows(0, -1, 0, (document.BottomMargin), writer.DirectContent);
        }
        public override void OnCloseDocument(PdfWriter writer, Document document)
        {
            base.OnCloseDocument(writer, document);
        }
    }

}
